package com.ziyueyuwen.xiniao.spi.impl;

import com.alibaba.fastjson.JSON;
import com.ziyueyuwen.xiniao.bean.homework.AnswerData;
import com.ziyueyuwen.xiniao.bean.homework.ItemData;
import com.ziyueyuwen.xiniao.bean.homework.QuestionInfo;
import com.ziyueyuwen.xiniao.spi.QuestionTypeService;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.List;

import static com.ziyueyuwen.xiniao.enums.QuestionTypeEnum.FILL_DRAG;

/**
 * @author jun 2018/12/18.
 */
public class FillDragImpl implements QuestionTypeService {

    @Override
    public void saveAnswer(String answerData, Integer questionNum, QuestionInfo questionInfo, List<AnswerData> answerDataList) {
        if (questionInfo.getQuestionType() == FILL_DRAG) {
            List<String> dataList = JSON.parseArray(answerData, String.class);
            AnswerData answerDataBean = new AnswerData();
            answerDataBean.setTotalScore(getTotalScore(questionInfo.getDragDataList()));
            answerDataBean.setQuestionType(FILL_DRAG);
            answerDataBean.setObjectiveAnswerList(dataList);
            answerDataBean.setCorrectScore(getCorrectScore(dataList, questionInfo));
            if (answerDataList.size() < questionNum) {
                answerDataList.add(answerDataBean);
            } else {
                answerDataList.set(questionNum - 1, answerDataBean);
            }
        }
    }

    /**
     * 获取得分, 填空题判分规则, 有序: 每个空有对应的答案, 错位算错; 无序: 只要答对即可
     *
     * @param studentAnswerList List<String>
     * @param questionInfo      QuestionInfo
     * @return Integer
     */
    private Integer getCorrectScore(List<String> studentAnswerList, QuestionInfo questionInfo) {
        List<String> correctAnswerList = questionInfo.getAnswerList();
        List<ItemData> itemDataList = questionInfo.getDragDataList();
        Boolean ifAnswerWithOrder = questionInfo.getIfAnswerWithOrder();
        int score = 0;
        if (ifAnswerWithOrder) {
            // 有序作答, 一一比较答案
            for (int i = 0; i < studentAnswerList.size(); i++) {
                Integer idx = NumberUtils.toInt(studentAnswerList.get(i), 0);
                if (idx > 0) {
                    ItemData itemData = itemDataList.get(idx - 1);
                    if (itemData.getIdx() == (i + 1)) {
                        score += itemData.getScore();
                    }
                }
            }
        } else {
            // 无序作答, 正确答案和学生答案取个交集
            correctAnswerList.retainAll(studentAnswerList);
            for (String s : correctAnswerList) {
                Integer idx = NumberUtils.toInt(s);
                ItemData itemData = itemDataList.get(idx - 1);
                score += itemData.getScore();
            }
        }
        return score;
    }

    /**
     * 计算本题总分
     *
     * @param list List
     * @return Integer
     */
    private Integer getTotalScore(List<ItemData> list) {
        int totalScore = 0;
        for (ItemData i : list) {
            totalScore += i.getScore();
        }
        return totalScore;
    }

}
